<?php
/**
 * Created by PhpStorm.
 * User: li gui gong
 * Date: 2017/5/1
 * Time: 16:34
 */
namespace Home\Model;
use Think\Model;
class ArticleModel extends Model{
    //允许提交的字段
    protected $insetFields = array('cat_id','user_id','title','content','pubtime','lastup','pic','thumb','is_display');
    //数据库中允许更新的字段
    protected $updateFields = array('art_id','cat_id','title','content','pic','thumb','is_display');
    //用到的验证规则
    protected $_validate = array(
        //下面的规则只有在添加时生效,修改时不生效,第六个参数代表 1：添加时验证 2：修改时 3：所有情况都验证
        //第四个参数的意思是0：存在字段就验证  1：必须验证  2：值不为空的时候验证
        //regex使用正则验证
        array('title', 'require', '标题不能为空！', 1, 'regex', 3),
        array('title', '1,30', '账号的值最长不能超过 30 个字符！', 1, 'length', 3),
        array('content', 'require', '文章内容不能为空！', 1, 'regex', 3),
        array('is_display', 'number', '是否显示必须是一个整数！', 2, 'regex', 3),
    );

    public function search($pageSize = 10)
    {
        /**************************************** 搜索 ****************************************/
        $where = array();
        //文章名搜索
        if($title = I('get.title'))
            $where['a.title'] = array('like', "%$title%");


        /**************************** 翻页 **************************/
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        // 修改分页样式
        $page->setConfig('header', '共%TOTAL_ROW%条数据，当前第%NOW_PAGE%/%TOTAL_PAGE%页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('first', '首页');
        $page->setConfig('last',   '尾页');
        $page->setConfig('next', '下一页');
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE%  %DOWN_PAGE% %END%');
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        //SELECT art_id,title,pubtime,is_display,c.username,b.catname from bk_article a LEFT JOIN  bk_cat b ON a.cat_id=b.cat_id LEFT JOIN bk_admin c ON c.id=a.user_id
        $data['data'] = $this->field('art_id,title,pubtime,is_display,c.realname,b.catname')->alias('a')->where($where)->join('LEFT JOIN zn_cat b ON a.cat_id=b.cat_id LEFT JOIN zn_admin c ON c.id=a.user_id')->order("art_id desc")->limit($page->firstRow.','.$page->listRows)->select();
        //$data['data'] = $this->field('a.*,GROUP_CONCAT(c.role_name) role_name')->alias('a')->where($where)->join('left join bk_admin_role b on a.id=b.admin_id left join bk_role c on c.id=b.role_id')->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
        $data['count'] = $count;
        return $data;
    }

    // 添加前
    protected function _before_insert(&$data, $option)
    {
        $arr = session('adminUser');
        $data['user_id'] = $arr['id'];
        $data['pubtime'] = time();
        $data['lastup'] = time();
        if(isset($_FILES['pic'])&&$_FILES['pic']['error']==0){
            $ret = uploadOne('pic','Article',array(array(480,300),array(207,120)));
            if($ret['ok']==1){
                $data['pic'] = $ret['images'][0];
                $data['thumb'] = $ret['images'][1];
                $data['min_pic'] = $ret['images'][2];
            }else{
                $this->error = $ret['error'];
                return false;
            }
        }
        //判断分类选择了没有
        if($data['cat_id']==0){
            $this->error = '请选择分类';
            return false;
        }
    }
    //添加后(给分类下的数量增加1)
    protected function _after_insert($data, $options){
        $cat_id = $data['cat_id'];
        $model = M('Cat');
        $model->where('cat_id='.$cat_id)->setInc('num',1); // 该分类下的数量加1
    }
    // 修改前
    protected function _before_update(&$data, $option)//data中没有隐藏域提交过来的信息
    {
        if(isset($_FILES['pic'])&&$_FILES['pic']['error']==0){
            $ret = uploadOne('pic','Article',array(array(480,300),array(207,120)));
            if($ret['ok']==1){
                $data['pic'] = $ret['images'][0];
                $data['thumb'] = $ret['images'][1];
                $data['min_pic'] = $ret['images'][2];
            }else{
                $this->error = $ret['error'];
                return false;
            }
            //如果上传了图片就把原来的删除掉
            deleteImage(array(
                I('post.old_pic'),
                I('post.old_thumb'),
                I('post.old_min_pic'),
            ));
        }
        //判断分类选择了没有
        if($data['cat_id']==0){
            $this->error = '请选择分类';
            return false;
        }
    }
    // 删除前
    protected function _before_delete($option)//option中有要删的id,表名,模型名
    {
        if(is_array($option['where']['art_id']))
        {
            $this->error = '不支持批量删除';
            return FALSE;
        }
        $images = $this->field('pic,thumb,min_pic')->find($option['where']['art_id']);
        deleteImage($images);//传一个一维数组
    }
}



